package com.xtravel.common.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.xtravel.R;
import com.xtravel.application.XtravelApplication;
import com.xtravel.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;

@SuppressLint("InflateParams")
public abstract class BaseFragment extends Fragment {
	int _layoutId;
	protected View _rootView;

	/**
	 * 消息按钮.
	 */
	protected LinearLayout imgMsgll;

	/**
	 * 消息数量.
	 */
	protected TextView tvMsgCount;

	protected Boolean isFirstLoad = true;

	protected static final String EX_KEY = "ex";

	/**
	 * 初始化控件.
	 */
	private void initView() {
		imgMsgll = (LinearLayout) _rootView.findViewById(R.id.header_msg);
		tvMsgCount = (TextView) _rootView.findViewById(R.id.header_msg_count);
	}


	/**
	 * 设置header标题.
	 *
	 * @param title 标题.
	 */
	protected void setHeaderTitle(String title) {
		TextView tvTitle = (TextView) _rootView.findViewById(R.id.header_title);
		if (tvTitle != null) {
			tvTitle.setText(StringUtils.isBlank(title) ? "" : title);
		}
	}

	/**
	 * 设置返回按钮可用与否.
	 *
	 * @param visibility 显示与否.
	 */
	protected void setBackBtVisible(int visibility) {
		Button btBack = (Button) _rootView.findViewById(R.id.header_back);
		if (btBack != null) {
			btBack.setVisibility(visibility);
		}
	}

	/**
	 * 设置消息返回按钮可用与否.
	 *
	 * @param visibility 显示与否.
	 */
	protected void setMsgImgBtnVisible(int visibility) {
		if (imgMsgll == null) {
			imgMsgll = (LinearLayout) _rootView.findViewById(R.id.header_msg);
		}
		imgMsgll.setVisibility(visibility);
	}

	public BaseFragment(int layoutId) {
		super();
		this._layoutId = layoutId;
	}



	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (_rootView == null) {
			isFirstLoad = true;
			_rootView = inflater.inflate(this._layoutId, container, false);
			// 初始化控件（主要是header控件）
			initView();
		} else {
			isFirstLoad = false;
		}
		return _rootView;
	}

	@Override
	public void onDestroyView() {
		if (_rootView.getParent() != null) {
			((ViewGroup) _rootView.getParent()).removeView(_rootView);
		}
		super.onDestroyView();
	}

	@Override public void onResume() {
		super.onResume();
	}

	public abstract void onFragmentChange(boolean isChange);

	public void onReClick() {
	}

	protected Handler handler = new Handler() {
		@Override public void handleMessage(Message msg) {
			Bundle bundle = msg.getData();
			if (bundle != null && bundle.getSerializable(EX_KEY) != null) {
				Exception e = (Exception) bundle.getSerializable(EX_KEY);
				if (e instanceof BusinessException) {
					// 业务异常
					XtravelApplication.showToastShort(e.getMessage());
				}  else if (e instanceof IOException) {
					// 网络异常
					XtravelApplication.showToastShort(">o_o< 网络似乎不能用了");
				} else {
                    e.printStackTrace();
				}
			}
		}
	};

    protected void handleException(Exception e) {
        Message msg = handler.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putSerializable(EX_KEY, e);
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

}